package com.enniu.alipay.api.data.config;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.enniu.alipay.api.atomikos.AtomikosJtaPlatform;
import com.enniu.alipay.api.data.util.DynamicDataSource;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.enniu.alipay.api.repository", entityManagerFactoryRef = "customerEntityManager", transactionManagerRef = "transactionManager")
@DependsOn("dynamicDatasource")
public class DatabaseConfig {



	@Bean(name = "customerEntityManager")
	@DependsOn({"transactionManager","dynamicDatasource"})
	public LocalContainerEntityManagerFactoryBean customerEntityManager() throws Throwable {
		HashMap<String, Object> properties = new HashMap<String, Object>();
		properties.put("hibernate.transaction.jta.platform", AtomikosJtaPlatform.class.getName());
		properties.put("javax.persistence.transactionType", "JTA");
		LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
		entityManager.setJtaDataSource(dynamicDatasource);
		entityManager.setJpaVendorAdapter(jpaVendorAdapter);
		entityManager.setPackagesToScan("com.enniu.alipay.api.domain");
		entityManager.setPersistenceUnitName("enniuPersistenceUnit");
		entityManager.setJpaPropertyMap(properties);
		return entityManager;
	}

	@Autowired
	private Environment env;
	@Autowired
	private DynamicDataSource dynamicDatasource;
	@Autowired
	private JpaVendorAdapter jpaVendorAdapter;
} 
